//
//  IEPageControl.m
//  LGIntellectExam
//
//  Created by dangwc on 2019/6/10.
//  Copyright © 2019 dangwc. All rights reserved.
//

#import "IEPageControl.h"
#import <Masonry/Masonry.h>



@interface IEPageControl ()

@property (nonatomic,strong) UIView *dotContentView;

@end

@implementation IEPageControl

-(instancetype)init{
    if (self = [super init]) {
    }
    return self;
}


-(UIView *)dotContentView{
    if (!_dotContentView) {
        _dotContentView = [[UIView alloc] init];
        _dotContentView.backgroundColor = [UIColor clearColor];
    }
    return _dotContentView;
}


-(void)setNumberOfPages:(NSInteger)numberOfPages{
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.dotContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    [self setupLayout];
    if (numberOfPages == 1 && self.hidesForSinglePage) {
        return;
    }
    _numberOfPages = numberOfPages;
    CGFloat dotwidth = 10;
    CGFloat dotInterval = 20;
    for (int i=0; i<_numberOfPages; i++) {
        UIView *dotView = [UIView new];
        dotView.tag = i;
        dotView.layer.cornerRadius = 5;
        dotView.layer.masksToBounds = YES;
        [self.dotContentView addSubview:dotView];
        [dotView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(i*(dotwidth+dotInterval));
            make.centerY.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(10, 10));
        }];
        if (i==_numberOfPages-1) {
            [self.dotContentView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(i*(dotwidth+dotInterval)+dotwidth);
            }];
        }
    }
    
    self.currentPage = 0;
}


-(void)setCurrentPage:(NSInteger)currentPage{
    [self.dotContentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (currentPage == idx) {
            obj.backgroundColor = self.currentPageIndicatorTintColor;
        }else{
            obj.backgroundColor = self.pageIndicatorTintColor;
        }
    }];
}


-(void)setHidesForSinglePage:(BOOL)hidesForSinglePage{
    _hidesForSinglePage = hidesForSinglePage;
}



-(void)setupLayout{
    
    [self addSubview:self.dotContentView];
    [self.dotContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(IE_ScreenWidth,5));
    }];
}

@end
